package fuzion24.device.vulnerability.test.adapter.viewholder;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.util.Linkify;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.nowsecure.android.vts.R;

import java.util.List;
import java.util.Locale;

public class RecyclerItemViewHolder extends RecyclerView.ViewHolder {

    private final TextView textviewTestName;
    private final TextView textviewTestResult;
    private final TextView textviewTestDescription;
    private final Button buttonShowDetails;

    public RecyclerItemViewHolder(final View parent, TextView textviewTestName, TextView textviewTestResult, TextView textviewTestDescription, Button buttonShowDetails) {
        super(parent);

        this.textviewTestName = textviewTestName;
        this.textviewTestResult = textviewTestResult;
        this.textviewTestDescription = textviewTestDescription;
        this.buttonShowDetails = buttonShowDetails;
    }

    public static RecyclerItemViewHolder newInstance(View view) {
        TextView textviewTestName = (TextView) view.findViewById(R.id.vulnerabity_name);
        TextView textviewTestResult = (TextView) view.findViewById(R.id.vulnerabity_result);
        TextView textviewTestDescription = (TextView) view.findViewById(R.id.vulnerabity_description);
        Button buttonShowDetails = (Button) view.findViewById(R.id.vulnerabity_show_details);

        return new RecyclerItemViewHolder(view, textviewTestName, textviewTestResult, textviewTestDescription, buttonShowDetails);
    }

    public void setItemTestName(CharSequence text) {
        textviewTestName.setText(text);
    }

    public void setItemTestResult(CharSequence text) {
        textviewTestResult.setText(text);
    }

    public void setItemTestResultColor(int color) {
        textviewTestResult.setTextColor(color);
    }

    public void setItemTestResultDescription(CharSequence description) {
        textviewTestDescription.setText(description);
    }

    public void setButtonShowDetailsClickListner(final Context context, final String title) {
        buttonShowDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new MaterialDialog.Builder(context)
                    .title(title)
                    .content(R.string.information_not_available)
                    .positiveText(R.string.dismiss)
                    .show();
            }
        });
    }

    public void setButtonShowDetailsClickListner(final Context context, final String title, final List<String> names, final String description, final String impact, final List<String> moreInfo, final List<String> patches, final Double cvssv2Score, final String cvedate) {
        buttonShowDetails.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MaterialDialog dialog = new MaterialDialog.Builder(context)
                    .title(title)
                    .customView(R.layout.dialog_vulnerability_details, true)
                    .positiveText(R.string.dismiss)
                    .show();

                View view = dialog.getCustomView();
                TextView textViewNames = (TextView) view.findViewById(R.id.dialog_vulnerabity_names);
                TextView textViewDescription = (TextView) view.findViewById(R.id.dialog_vulnerabity_description);
                TextView textViewImpact = (TextView) view.findViewById(R.id.dialog_vulnerabity_impact);
                TextView textViewMoreInfo = (TextView) view.findViewById(R.id.dialog_vulnerabity_more_info);
                TextView textViewPatches = (TextView) view.findViewById(R.id.dialog_vulnerabity_patches);
                TextView textViewCvssv2 = (TextView) view.findViewById(R.id.dialog_vulnerabity_cvssv2);
                TextView textViewCvedate = (TextView) view.findViewById(R.id.dialog_vulnerabity_cvedate);

                textViewNames.setText(TextUtils.join("\n", names));
                textViewDescription.setText(description);
                textViewImpact.setText(impact);

                textViewMoreInfo.setText(TextUtils.join("\n", moreInfo));
                Linkify.addLinks(textViewMoreInfo, Linkify.WEB_URLS);

                textViewPatches.setText(TextUtils.join("\n", patches));
                Linkify.addLinks(textViewPatches, Linkify.WEB_URLS);

                textViewCvssv2.setText(String.format(Locale.getDefault(), "%.1f", cvssv2Score));
                textViewCvedate.setText(cvedate);
            }
        });
    }

}
